<?php

namespace app\index\controller;

use app\common\entity\Goods;
use app\common\entity\MywalletLog;
use app\common\entity\MachineList;
use app\common\entity\Orders;
use app\common\entity\Proportion;
use app\common\entity\UserLevelSend;
use app\common\entity\UserPackage;
use app\index\model\SendCode;
use think\Db;
use think\Exception;
use think\Request;
use app\common\entity\User;


class Rushgoods extends Base
{
    /**
     * 福多多展示商品
     */
    public function goods_list(Request $request)
    {
        $list = DB::table('rush_order')->where('status', 1)->field('id,goods_title,goods_now_integral,goods_total_need_integral,goods_pic,sort')->order('sort desc')->select();
        foreach ($list as $key => $value) {
            $list[$key]['plan_percent'] = bcmul(bcdiv($value['goods_now_integral'], $value['goods_total_need_integral'], 4), 100, 2);

            // $list[$key]['goods_pic'] = json_decode($value['goods_pic'])[0];

        }
        return json(['code' => 0, 'message' => '请求成功', 'info' => $list]);
    }

    /***
     * 商品详情
     */
    public function goods_detail(Request $request)
    {
        $id = $request->post('id') + 0;
        $list = DB::table('rush_order')->where('id', $id)->where('status', 'in', '1,2')->find();
        if (empty($list)) {
            return json(['code' => '-1', 'message' => '活动已结束']);
        }
        $list['createtime'] = date('Y-m-d H:i', $list['createtime']);
        $list['plan_percent'] = bcmul(bcdiv($list['goods_now_integral'], $list['goods_total_need_integral'], 4), 100, 2);
        $list['goods_pic'] = $list['goods_pic'];
        $list['goods_content'] = htmlspecialchars_decode($list['goods_content']);
        $uid = $this->userId;
        //查看是否参与过
        $order_list = DB::table('rush_order_user_list')->where('rush_order_id', $id)->where('user_id', $uid)->count();
        // if($order_list){
        //     $user_order =1;

        // }else{
        //      $user_order =0;
        // }
        if ($list['status'] == 2) {
            $list['gain'] = DB::table('user')->alias('u')
                ->join('rush_order_user_list ol', 'ol.user_id=u.id', 'LEFT')
                ->where('u.id', $list['gain_id'])
                ->where('ol.rush_order_id', $list['id'])
                ->field('u.mobile,ol.number')
                ->find();


            $list['gain']['mobile'] = substr($list['gain']['mobile'], 0, 3) . '****' . substr($list['gain']['mobile'], -4);
        }
        return json(['code' => 0, 'message' => '请求成功', 'info' => $list, 'order_list' => $order_list]);
    }


    public function opennumbertest()
    {
        self::opennumber(3);
    }

    /**
     * 开奖
     */
    static function opennumber($id)
    {
        $list = DB::table('rush_order')->where('id', $id)->where('status', 'in', '1')->find();
        if (empty($list)) {
            return;
        }
        if ($list['goods_total_need_integral'] == $list['goods_now_integral']) {
            $user_list = DB::table('rush_order_user_list')->where('rush_order_id', $id)->where('is_win',1)->find();
            if($user_list){
                DB::table('rush_order')->where('id', $id)->update(['status' => 2, 'gain_id' => $user_list['user_id'], 'finishtime' => time()]);
            }else{
                $user_list = DB::table('rush_order_user_list')->where('rush_order_id', $id)->select();
                $rand = mt_rand(1, $list['goods_total_number']);
                DB::table('rush_order')->where('id', $id)->update(['status' => 2, 'gain_id' => $user_list[$rand - 1]['user_id'], 'finishtime' => time()]);
            }
           
        }
    }

    /**
     * 商城订单
     */
    public function store_list(Request $request)
    {
        $uid = $this->userId;
        $type = $request->post('type') ?? '0';
        $all_type = $request->post('all_type') ?? '1';
        $limit = $request->post('limit') ?? '10';
        $page = $request->post('page') ?? '1';
        $gain = '1=1';
        switch ($type) {
            case '0':
                $status = '2';
//                $status = [0,1,2];
                break;
            case '1':
                $status = '1';
                break;
            case '2':
                $status = '2';
                if ($all_type == 1) {
                    $gain = 'o.gain_id = ' . $uid;
                } else {
                    $gain = 'o.gain_id <> ' . $uid;
                }

                break;
        }
        if ($type == 0) {
            $list = DB::table('rush_order_user_list')->alias('ro')
                ->join('rush_order o', 'o.id=ro.rush_order_id', 'LEFT')
                ->where('o.status', 'in', $status)
                ->where($gain)
                ->field("o.*,ro.createtime as ctime,ro.number,count('o.id') as total")
                ->order('ro.createtime desc')
                ->where('ro.user_id',$uid)//
                ->group('o.id')
                ->limit($limit)
                ->page($page)
                ->select();
            foreach ($list as $key => $value) {
                $list[$key]['createtime'] = date('Y-m-d H:i:s', $value['ctime']);
                $list[$key]['plan_percent'] = bcmul(bcdiv($value['goods_now_integral'], $value['goods_total_need_integral'], 4), 100, 2);
                $list[$key]['gain_mobile'] = DB::table('user')->where('id', $value['gain_id'])->value('mobile');
                $list[$key]['gain_nick_name'] = DB::table('user')->where('id', $value['gain_id'])->value('nick_name');
                $list[$key]['gain_mobile'] = substr($list[$key]['gain_mobile'], 0, 3) . '****' . substr($list[$key]['gain_mobile'], -4);
                $list[$key]['number'] = DB::table('rush_order_user_list')->where('user_id', $value['gain_id'])->where('rush_order_id', $value['id'])->order('id desc')->value('number');
                $list[$key]['uid'] = $uid;
            }
        } else {
            $list = DB::table('rush_order_user_list')->alias('ro')
                ->join('rush_order o', 'o.id=ro.rush_order_id', 'LEFT')
                ->where('ro.user_id', $uid)
                ->where('o.status', 'in', $status)
                ->where($gain)
                ->field("o.*,ro.createtime as ctime,ro.number,count('o.id') as total")
//                            ->field("o.*,ro.createtime as ctime,ro.number")
                ->order('ro.createtime desc')
                ->group('o.id')
                ->limit($limit)
                ->page($page)
                ->select();
            foreach ($list as $key => $value) {
                $list[$key]['createtime'] = date('Y-m-d H:i:s', $value['ctime']);
                $list[$key]['plan_percent'] = bcmul(bcdiv($value['goods_now_integral'], $value['goods_total_need_integral'], 4), 100, 2);
                $list[$key]['gain_mobile'] = DB::table('user')->where('id', $value['gain_id'])->value('mobile');
                $list[$key]['gain_nick_name'] = DB::table('user')->where('id', $value['gain_id'])->value('nick_name');
                $list[$key]['gain_mobile'] = substr($list[$key]['gain_mobile'], 0, 3) . '****' . substr($list[$key]['gain_mobile'], -4);
                $list[$key]['number'] = DB::table('rush_order_user_list')->where('user_id', $value['gain_id'])->where('rush_order_id', $value['id'])->order('id desc')->value('number');
                $list[$key]['uid'] = $uid;
            }
        }

        return json(['code' => 0, 'message' => '请求成功', 'info' => $list]);

    }

    /**
     * 填写收货地址
     */
    public function add_address(Request $request)
    {
        $uid = $this->userId;
        $id = $request->post('id') + 0;
        $address = $request->post('address');
        $address_tel = $request->post('address_tel');
        $address_name = $request->post('address_name');
        if (empty($address) || empty($address_tel) || empty($address_name)) {
            return json(['code' => '-1', 'message' => '请填写信息']);
        }
        $list = DB::table('rush_order')->where('id', $id)->where('status', 2)->find();
        if (empty($list)) {
            return json(['code' => '-1', 'message' => '活动未结束']);
        }
        if ($list['gain_id'] != $uid) {
            return json(['code' => '-1', 'message' => '中奖者不是你哦']);
        }
        DB::table('rush_order')->where('id', $id)->update([
            'address' => $address,
            'address_tel' => $address_tel,
            'address_name' => $address_name,
        ]);
        return json(['code' => '0', 'message' => '填写成功']);

    }

    /**
     * 确定收货
     */
    public function goods_finish(Request $request)
    {
        $uid = $this->userId;
        $id = $request->post('id') + 0;
        $list = DB::table('rush_order')->where('id', $id)->where('status', 2)->where('goods_status', 1)->find();
        if (empty($list)) {
            return json(['code' => '-1', 'message' => '信息有误']);
        }
        if ($list['gain_id'] != $uid) {
            return json(['code' => '-1', 'message' => '中奖者不是你哦']);
        }
        DB::table('rush_order')->where('id', $id)->update([
            'goods_status' => 2,
        ]);
        return json(['code' => '0', 'message' => '收货成功']);
    }

    /**
     * 所有参与记录
     */
    public function draw_list(Request $request)
    {
        $uid = $this->userId;
        $limit = $request->post('limit') ?? '10';
        $page = $request->post('page') ?? '1';
        $list = DB::table('rush_order_user_list')->alias('ro')
            ->join('user u', 'u.id=ro.user_id', 'LEFT')
            ->where('ro.user_id', $uid)
            ->field('u.mobile,u.nick_name,ro.createtime,ro.integral,ro.rush_order_id,ro.number')
            ->limit($limit)
            ->page($page)
            ->select();
        foreach ($list as $key => $value) {
            $list[$key]['createtime'] = date('Y-m-d H:i:s', $value['createtime']);
            $list[$key]['mobile'] = substr($value['mobile'], 0, 3) . '****' . substr($value['mobile'], -4);
        }
        return json(['code' => 0, 'message' => '请求成功', 'info' => $list]);

    }

    /**
     * 我的幸运号
     */
    public function my_draw(Request $request)
    {
        $uid = $this->userId;
        $limit = $request->post('limit') ?? '10';
        $id = $request->post('id') ?? '1';
        $page = $request->post('page') ?? '1';
        $list = DB::table('rush_order')->alias('ro')
            ->join('rush_order_user_list roul', 'roul.rush_order_id = ro.id', 'LEFT')
            ->where('ro.id', $id)
            ->where('roul.user_id', $uid)
            ->field('ro.id,ro.goods_total_need_integral,roul.number,ro.stagehao')
            ->limit($limit)
            ->page($page)
            ->select();
        return json(['code' => 0, 'message' => '请求成功', 'info' => $list]);

    }

    /**
     * 商城订单
     */
    public function store_detail_list(Request $request)
    {
        $uid = $this->userId;
        $limit = $request->post('limit') ?? '10';
        $page = $request->post('page') ?? '1';
        $list = DB::table('rush_order')->alias('ro')
            ->join('rush_order_user_list roul', 'roul.rush_order_id = ro.id', 'LEFT')
            ->join('user u', 'u.id = ro.gain_id', 'LEFT')
            ->where('ro.status', 2)
            ->field('ro.id,ro.goods_total_need_integral,roul.number,ro.finishtime,u.mobile')
            ->limit($limit)
            ->page($page)
            ->select();
        foreach ($list as $key => $value) {
            $list[$key]['finishtime'] = date('Y-m-d H:i:s', $value['finishtime']);
            $list[$key]['mobile'] = substr($value['mobile'], 0, 3) . '****' . substr($value['mobile'], -4);
        }
        return json(['code' => 0, 'message' => '请求成功', 'info' => $list]);

    }

    /**
     * 所有参与记录
     */
    public function all_part_record(Request $request)
    {
        $uid = $this->userId;
        $limit = $request->post('limit') ?? '10';
        $page = $request->post('page') ?? '1';
        $id = $request->post('id') + 0;
        $list = DB::table('rush_order_user_list')
            ->alias('ul')
            ->join('user u', 'u.id = ul.user_id')
            ->join('rush_order ro', 'ro.id = ul.rush_order_id')
            ->where('ul.rush_order_id', $id)
            ->field('ul.*,u.mobile,ro.stagehao,ro.goods_total_number')
            ->order('ul.id desc')
            ->limit($limit)
            ->page($page)
            ->select();
        foreach ($list as $key => $value) {
            $list[$key]['createtime'] = date('Y-m-d H:i:s', $value['createtime']);
            $list[$key]['mobile'] = substr($value['mobile'], 0, 3) . '****' . substr($value['mobile'], -4);

        }
        return json(['code' => 0, 'message' => '请求成功', 'info' => $list]);

    }

    /**
     * 福多多图片
     */
    public function index_logo()
    {
        $list = DB::table('config')->where('key', 'index_logo')->value('value');
        return json(['code' => 0, 'message' => '请求成功', 'info' => $list]);

    }
}
